from selenium import webdriver
from selenium.common import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
import time

timeunit = 10
def login_tongyishenfen(driver, username, password):
    loginCheckBtn = WebDriverWait(driver, timeunit).until(
        EC.element_to_be_clickable((By.XPATH, '/html/body/div/div[1]/div[1]/div[3]/div[1]/img[2]'))
    )
    # 点击切换登录控件
    loginCheckBtn.click()

    # 显式等待，直到用户名输入框可见
    username_input = WebDriverWait(driver, timeunit).until(
        EC.visibility_of_element_located((By.XPATH, '//*[@id="username"]//div[@class="para-input"][1]//input'))
    )

    # 输入用户名
    username_input.send_keys(username)  # 替换为实际用户名

    # 显式等待，直到密码输入框可见
    password_input = WebDriverWait(driver, timeunit).until(
        EC.visibility_of_element_located((By.XPATH, '//*[@id="username"]//div[@class="para-input"][2]//input'))
    )

    # 输入密码
    password_input.send_keys(password)  # 替换为实际密码

    # input_element = WebDriverWait(driver, timeunit).until(
    #     EC.visibility_of_element_located((By.XPATH, '//*[@id="username"]/div/div[1]/div[1]/div/input'))
    # )
    # input_element.send_keys("15027783637")
    #
    # password_element = WebDriverWait(driver, timeunit).until(
    #     EC.visibility_of_element_located((By.XPATH, '//*[@id="username"]/div/div[2]/div[1]/div/input'))
    # )
    # input_element.send_keys("geqiang860127")
    #
    # #//*[@id="username"]/button
    # loginBtn = WebDriverWait(driver, timeunit).until(
    #     EC.element_to_be_clickable((By.XPATH, '*[@id="username"]/button'))
    # )
    # # 点击登录控件
    # loginBtn.click()
    login_button = WebDriverWait(driver, timeunit).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="username"]//button[contains(@class, "para-btn-login")]'))
    )

    # 点击登录按钮
    login_button.click()


def login_jiaoyuyun(driver):
    main_window = driver.current_window_handle
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    jiaoyuyun_button = WebDriverWait(driver, 50).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@title="教育云平台"]'))
    )
    jiaoyuyun_button.click()
    all_windows = driver.window_handles
    # 切换到新窗口/标签
    for window in all_windows:
        if window != main_window:
            driver.switch_to.window(window)
            break


def login_yijiaoyan(driver):
    zhihuijiaoyu_tap = WebDriverWait(driver, 20).until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="tab-2"]'))
    )
    # 打印元素的文本，确保已定位到
    print(zhihuijiaoyu_tap.text)
    # 点击 '智慧教学类' 选项
    zhihuijiaoyu_tap.click()
    #
    yijiaoyan_button = WebDriverWait(driver, timeunit).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@id="pane-2"]/ul/li[10]/a/img'))
    )
    main_window = driver.current_window_handle
    yijiaoyan_button.click()
    all_windows = driver.window_handles

    # 切换到新窗口/标签
    if all_windows[-1] != main_window:
        driver.switch_to.window(all_windows[-1])


def search_jiaoyan(driver):
    search_box = WebDriverWait(driver, timeunit).until(
        EC.presence_of_element_located((By.XPATH, '//input[contains(@placeholder, "教研名称、人员、校区")]'))
    )
    search_box.click()
    search_box.send_keys("技术教研")
    search_box.send_keys(Keys.RETURN)

    #进入教研详情页
    jiaoyan_res = WebDriverWait(driver, timeunit).until(
        EC.presence_of_element_located((By.XPATH, '//div[@class="eduBox"]//div[@class="showEduBox"]//div[@class="container"][1]'))
    )
    main_window = driver.current_window_handle
    jiaoyan_res.click()
    all_windows = driver.window_handles

    # 切换到新窗口/标签
    if all_windows[-1] != main_window:
        driver.switch_to.window(all_windows[-1])

def enter_jiaoyan(driver):
    jinrujiaoyan_btn = WebDriverWait(driver, timeunit).until(
        EC.presence_of_element_located((By.XPATH, '//span[@class="btn" and text()="进入教研"]'))
    )
    jinrujiaoyan_btn.click()


def startTest(username, password):
    chrome_options = Options()

    # 添加无痕模式参数,关闭无痕模式https默认转化参数
    chrome_options.add_argument("--incognito")
    chrome_options.add_argument("--disable-features=HttpsFirstModeIncognito")
    # 启动浏览器
    driver = webdriver.Chrome(options=chrome_options)
    driver.get("http://220.163.11.78:8087/login/")

    try:
        login_tongyishenfen(driver, "15027783637", "geqiang860127")
        login_jiaoyuyun(driver)
        login_yijiaoyan(driver)
        search_jiaoyan(driver)
        enter_jiaoyan(driver)
    except TimeoutException:
        with open("temp.html", "w", encoding="utf-8") as f:
            f.write(driver.page_source)
    input("按 Enter 键关闭浏览器...")
    # 关闭浏览器
    driver.quit()


if __name__ == '__main__':
    chrome_options = Options()

    # 添加无痕模式参数
    chrome_options.add_argument("--incognito")
    # chrome_options.add_argument("--disable-infobars")
    # chrome_options.add_argument("start-maximized")
    # chrome_options.add_argument("--disable-extensions")
    # chrome_options.add_argument("--disable-popup-blocking")
    # chrome_options.add_argument('--allow-running-insecure-content')
    # chrome_options.add_argument(
    #     "--unsafely-treat-insecure-origin-as-secure=http://220.163.11.78:8087/login/")  # Replace example.com with your site's domain
    chrome_options.add_argument("--disable-features=HttpsFirstModeIncognito")
    # chrome_options.add_argument("--disable-https-first-mode-incognito")
    # 启动浏览器
    driver = webdriver.Chrome(options=chrome_options)
    driver.get("http://220.163.11.78:8087/login/")



    try:
        login_tongyishenfen(driver, "15027783637", "geqiang860127")
        login_jiaoyuyun(driver)
        login_yijiaoyan(driver)
        search_jiaoyan(driver)
        enter_jiaoyan(driver)
    except TimeoutException:
        with open("temp.html", "w", encoding="utf-8") as f:
            f.write(driver.page_source)
    input("按 Enter 键关闭浏览器...")
    # 关闭浏览器
    driver.quit()
